home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Magazine / C_Tutorial / Part-9 / wb0 / main.c < prev    next >
C/C++ Source or Header  |  1998-02-01  |  4KB  |  171 lines

  1. #include<dos/rdargs.h>
  2. #include<exec/libraries.h>
  3. #include<workbench/startup.h>
  4.  
  5. #include<stdio.h>
  6.  
  7. #include<clib/dos_protos.h>
  8. #include<clib/exec_protos.h>
  9.  
  10. #include "gui.h"
  11. #include "idcmp.h"
  12. #include "loadsave.h"
  13. #include "arexx.h"
  14.  
  15. /* The library base global variables */
  16. /* (The different style of opening libraries requires these to be initialised to NULL) */
  17. struct Library* GfxBase = NULL;
  18. struct Library* IntuitionBase = NULL;
  19. struct Library* GadToolsBase = NULL;
  20. struct Library* AslBase = NULL;
  21. struct Library* DosBase = NULL;
  22. struct Library* IFFBase = NULL;
  23. struct Library* RexxSysBase = NULL;
  24.  
  25. /* Need to give prototypes for our functions */
  26. static int  createAll(void);
  27. static void freeAll(void);
  28. static int  openLibs(void);
  29. static void closeLibs(void);
  30.  
  31. /* The alternative "main()" for Workbench startup */
  32. void wbmain(struct WBStartup*);
  33.  
  34. /* The shared starting point of our program */
  35. static void realmain(void);
  36.  
  37. #define ARGS_TEMPLATE "DEPTH/N,PORTNAME"
  38.  
  39. enum ARGS { ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  40.  
  41. #define DEFAULT_PORTNAME "HELLOPAINTER"
  42. #define DEFAULT_DEPTH    (4)
  43.  
  44. /* The CLI starting point for StormC, but the general start for SAS/C */
  45. void main(int argc, char** argv)
  46. {
  47.     /* argc should never be zero: SAS/C uses this to indicate WB start */
  48.   if(argc == 0)
  49.         wbmain((struct WBStartup*)argv);
  50.     else
  51.         realmain();
  52. }
  53.  
  54. /* The WB starting point for StormC */
  55. void wbmain(struct WBStartup* wbmsg)
  56. {
  57.     /* WB-specific startup could go here */
  58.     realmain();
  59. }
  60.  
  61. /* The start of the program */
  62. static void realmain()
  63. {
  64.     if(createAll())
  65.         handleIDCMP();
  66.     freeAll();
  67. }
  68.  
  69. static struct RDArgs* rdargs = NULL;
  70.  
  71. static int createAll()
  72. {
  73.   LONG args[NUM_ARGS];
  74.   int i;
  75.   /* Initialise our args to NULL */
  76.   /* (This way we will know if an argument was specified) */
  77.   for(i=0; i<NUM_ARGS; i++)
  78.       args[i] = NULL;
  79.   if(openLibs())
  80.     {
  81.       if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  82.         {
  83.             char* portname = (char*)(args[ARG_PORTNAME]);
  84.         LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  85.         UBYTE depth;
  86.             /* Use the default if an argument was not specified */
  87.         if(portname == NULL)
  88.             portname = DEFAULT_PORTNAME;
  89.         if(depthptr == NULL)
  90.             depth = DEFAULT_DEPTH;
  91.         else
  92.             depth = (UBYTE)(*depthptr);
  93.             return createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0);
  94.         }
  95.         else
  96.             printf("Error: could not read arguments\n");
  97.     }
  98.     return FALSE;
  99. }
  100.  
  101. static void freeAll()
  102. {
  103.     freeReqs();
  104.     closeGUI();
  105.     freeArgs();
  106.     freeARexxPort();
  107.     if(rdargs)
  108.         FreeArgs(rdargs);
  109.     closeLibs();
  110. }
  111.  
  112. /* Try to open all the libraries -- return TRUE on success */
  113. static int openLibs()
  114. {
  115.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  116.     {
  117.         printf("Error: could not open graphics.library\n");
  118.         return FALSE;
  119.     }
  120.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  121.     {
  122.         printf("Error: could not open intuition.library\n");
  123.         return FALSE;
  124.     }
  125.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  126.     {
  127.         printf("Error: could not open gadtools.library\n");
  128.         return FALSE;
  129.     }
  130.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  131.     {
  132.         printf("Error: could not open asl.library\n");
  133.         return FALSE;
  134.     }
  135.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  136.     {
  137.         printf("Error: could not open dos.library\n");
  138.         return FALSE;
  139.     }
  140.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  141.     {
  142.         printf("Error: could not open iff.library\n");
  143.         return FALSE;
  144.     }
  145.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  146.     {
  147.         printf("Error: could not open rexxsyslib.library\n");
  148.         return FALSE;
  149.     }
  150.   return TRUE;
  151. }
  152.  
  153. /* Close any open library */
  154. static void closeLibs()
  155. {
  156.     if(RexxSysBase)
  157.         CloseLibrary(RexxSysBase);
  158.     if(IFFBase)
  159.         CloseLibrary(IFFBase);
  160.     if(DosBase)
  161.         CloseLibrary(DosBase);
  162.     if(AslBase)
  163.         CloseLibrary(AslBase);
  164.     if(GadToolsBase)
  165.         CloseLibrary(GadToolsBase);
  166.     if(IntuitionBase)
  167.         CloseLibrary(IntuitionBase);
  168.     if(GfxBase)
  169.         CloseLibrary(GfxBase);
  170. }
  171.